package cn.bocweb.spinning.view.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.MyClassNewBean;
import cn.bocweb.spinning.util.Constant;
import cn.bocweb.spinning.util.DeviceUtil;
import cn.bocweb.spinning.view.activity.BestBean;
import cn.bocweb.spinning.view.activity.CountActivity;
import cn.bocweb.spinning.view.activity.TestDetailActivity;
import cn.bocweb.spinning.view.common.MagnificentChart;
import cn.bocweb.spinning.view.common.MagnificentChartItem;

/**
 * Created by chenchangxing on 16/8/15.
 */
public class MySchoolListAdapter extends BaseExpandableListAdapter {

    List<String> titles;
    List<BestBean> bestBeen;
    List<MyClassNewBean.RecordBean> recordBeen;
    List<MyClassNewBean.MatchBean.RecordBean> matchBean;

    public MySchoolListAdapter(List<String> titles, List<MyClassNewBean.MatchBean.RecordBean> matchBean, List<MyClassNewBean.RecordBean> recordBeen, List<BestBean> bestBeen) {
        this.titles = titles;
        this.bestBeen = bestBeen;
        this.recordBeen = recordBeen;
        this.matchBean = matchBean;
    }

    @Override
    public int getGroupCount() {
        return 3;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        switch (groupPosition) {
            case 0:
                return matchBean.size();
            case 1:
                return recordBeen.size();
            case 2:
                return bestBeen.size();
        }
        return 0;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return null;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return null;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return 0;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ViewHolderGroup group;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_my_school_group, parent, false);
            group = new ViewHolderGroup(convertView);
            convertView.setTag(group);
        } else {
            group = (ViewHolderGroup) convertView.getTag();
        }

        if (!titles.isEmpty()) {
            group.textviewTitle.setText(titles.get(groupPosition));
        }

        if (isExpanded) {
            group.imageViewSelect.setImageResource(R.mipmap.group_select);
        } else {
            group.imageViewSelect.setImageResource(R.mipmap.group_unselect);
        }

        return convertView;
    }


    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, final ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            switch (getChildType(groupPosition, childPosition)) {
                case 0:
                    convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_my_school_child_0, parent, false);
                    holder = new ViewHolder(convertView, getChildType(groupPosition, childPosition));
                    convertView.setTag(holder);
                    break;
                case 1:
                    convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_my_school_child_1, parent, false);
                    holder = new ViewHolder(convertView, getChildType(groupPosition, childPosition));
                    convertView.setTag(holder);
                    break;
                case 2:
                    convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_my_school_child_2, parent, false);
                    holder = new ViewHolder(convertView, getChildType(groupPosition, childPosition));
                    convertView.setTag(holder);
                    break;
                default:
                    convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_my_school_child_0, parent, false);
                    holder = new ViewHolder(convertView, getChildType(groupPosition, childPosition));
                    convertView.setTag(holder);
                    break;
            }
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        switch (getChildType(groupPosition, childPosition)) {
            case 0:
                final MyClassNewBean.MatchBean.RecordBean recordBean = matchBean.get(childPosition);
                if (recordBean.getBest() == null) {
                    holder.textviewTitle.setText(recordBean.getTitle());
                    holder.textviewEmpty.setVisibility(View.VISIBLE);
                    holder.linearLayoutScore.setVisibility(View.GONE);
                    holder.relativeLayoutDetail.setVisibility(View.GONE);
                } else {
                    holder.textviewTitle.setText(recordBean.getTitle());
                    holder.textviewEmpty.setVisibility(View.GONE);
                    holder.linearLayoutScore.setVisibility(View.VISIBLE);
                    holder.score.setText(recordBean.getBest().getScore());
                    holder.textviewLevel.setText(recordBean.getBest().getSubtitle());
                    holder.scoreUsedTime.setText(getTimeStr(recordBean.getBest().getSpend_time()));
                    holder.relativeLayoutDetail.setVisibility(View.VISIBLE);
                    holder.relativeLayoutDetail.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(parent.getContext(), TestDetailActivity.class);
                            //下面判断必改 判断是不是初赛还是复赛
                            intent.putExtra(Constant.TO_TEST_DETAIL_IS_ACITVITY, true);
                            if (matchBean.get(childPosition).getId().equals("16")) {
                                intent.putExtra(Constant.TO_TEST_DETAIL_IS_SECOND, true);
                            }
                            intent.putExtra(Constant.TEST_DETAIL, recordBean.getBest().getTestid());
                            intent.putExtra(Constant.SCHOOL_TO_TEST, titles.get(groupPosition) + recordBean.getTitle());
                            intent.putExtra(Constant.SCHOOL_TO_TEST_DETAIL_LIB, recordBean.getBest().getLibtype());
                            parent.getContext().startActivity(intent);
                        }
                    });
                }
                break;
            case 1:
                final MyClassNewBean.RecordBean recordBean1 = recordBeen.get(childPosition);
                initMagnificentChart(holder.roundProcess, parent.getContext(), recordBean1, holder.btnProcessDetail);
                holder.questionTitle.setText(recordBean1.getTitle());
                holder.countRight.setText(String.valueOf(recordBean1.getRight()));
                holder.countWrong.setText(String.valueOf(recordBean1.getWrong()));
                holder.countMiss.setText(String.valueOf(recordBean1.getBlank()));
                holder.btnProcessDetail.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(parent.getContext(), CountActivity.class);
                        intent.putExtra(Constant.COUNT_ACITIVYT, recordBean1);
                        parent.getContext().startActivity(intent);
                    }
                });
                break;
            case 2:
                final BestBean bean = bestBeen.get(childPosition);
                if (TextUtils.isEmpty(bean.getId())) {
                    holder.textviewTitle2.setText(bean.getTitle());
                    holder.textViewEmpty2.setVisibility(View.VISIBLE);
                    holder.linearLayoutScore2.setVisibility(View.GONE);
                    holder.relativeLayoutDetail2.setVisibility(View.GONE);
                } else {
                    holder.linearLayoutScore2.setVisibility(View.VISIBLE);
                    holder.textViewEmpty2.setVisibility(View.GONE);
                    holder.textviewTitle2.setText(bean.getTitle());
                    holder.score2.setText(bean.getScore());
                    holder.scoreUsedTime2.setText(getTimeStr(bestBeen.get(childPosition).getSpend_time()));
                    holder.textviewLevel2.setText(bean.getSubtitle());
                    holder.relativeLayoutDetail2.setVisibility(View.VISIBLE);
                    holder.relativeLayoutDetail2.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(parent.getContext(), TestDetailActivity.class);
                            intent.putExtra(Constant.TEST_DETAIL, bean.getTestid());
                            intent.putExtra(Constant.SCHOOL_TO_TEST_DETAIL_LIB, bean.getLibtype());
                            intent.putExtra(Constant.SCHOOL_TO_TEST, bean.getTitle());
                            parent.getContext().startActivity(intent);
                        }
                    });
                }
                break;
        }

        return convertView;
    }

    private void initMagnificentChart(MagnificentChart roundProcess, Context context, MyClassNewBean.RecordBean bean, RelativeLayout btn) {
        List<MagnificentChartItem> magnificentChartItems = new ArrayList<>();

        DecimalFormat df0 = new DecimalFormat("######0");
        DecimalFormat df1 = new DecimalFormat("######0.0");
        DecimalFormat df2 = new DecimalFormat("######0.00");

        double total = bean.getTotal();
        double right = bean.getRight() * 100 / total;
        double wrong = bean.getWrong() * 100 / total;
        double miss = 100 - right - wrong;

        if (bean.getRight() == 0 && bean.getWrong() == 0 && bean.getTotal() == 0 && bean.getBlank() == 0) {
            roundProcess.setText("0%");
            MagnificentChartItem item2 = new MagnificentChartItem("未做", 100, context.getResources().getColor(R.color.colorPrimary));
            magnificentChartItems.add(item2);
            btn.setVisibility(View.GONE);
        } else {
            roundProcess.setText(df1.format(right + wrong) + "%");
            MagnificentChartItem item = new MagnificentChartItem("做对", right , context.getResources().getColor(R.color.green));
            MagnificentChartItem item1 = new MagnificentChartItem("做错", wrong , context.getResources().getColor(R.color.red));
            MagnificentChartItem item2 = new MagnificentChartItem("未做", miss, context.getResources().getColor(R.color.colorPrimary));
            magnificentChartItems.add(item);
            magnificentChartItems.add(item1);
            magnificentChartItems.add(item2);
            btn.setVisibility(View.VISIBLE);
        }

        DeviceUtil.Log("right = " + right + " wrong = " + wrong + " miss = " + miss);



        roundProcess.setChartItemsList(magnificentChartItems);
        roundProcess.setMaxValue(100);
    }

    private String getTimeStr(String spend_time) {
        int time = Integer.parseInt(spend_time);
        if(time < 60) {
            return spend_time + "秒";
        } else {
            return String.valueOf(time / 60) + "分" + String.valueOf(time % 60) + "秒";
        }
    }

    @Override
    public int getChildType(int groupPosition, int childPosition) {
        switch (groupPosition) {
            case 0:
                return 0;
            case 1:
                return 1;
            case 2:
                return 2;
            default:
                return 0;
        }
    }

    @Override
    public int getChildTypeCount() {
        return 3;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }


    static class ViewHolderGroup {
        @Bind(R.id.textview_title)
        TextView textviewTitle;
        @Bind(R.id.imageview_select)
        ImageView imageViewSelect;

        ViewHolderGroup(View view) {
            ButterKnife.bind(this, view);
        }
    }

    private String getGroupTitle(int groupPosition) {
        switch (groupPosition) {
            case 0:
                return "比赛成绩";
            case 1:
                return "学习进度";
            case 2:
                return "最好成绩";
            default:
                return "未知错误";
        }
    }

    static class ViewHolder {
        // type = 0
        TextView textviewTitle;
        TextView textviewLevel;
        TextView score;
        TextView scoreUsedTime;
        RelativeLayout relativeLayoutDetail;
        TextView textviewEmpty;
        LinearLayout linearLayoutScore;

        // type = 1
        TextView questionTitle;
        MagnificentChart roundProcess;
        TextView countRight;
        TextView countWrong;
        TextView countMiss;
        RelativeLayout btnProcessDetail;

        // type = 2
        TextView textviewTitle2;
        TextView textviewLevel2;
        TextView score2;
        TextView scoreUsedTime2;
        RelativeLayout relativeLayoutDetail2;
        TextView textViewEmpty2;
        TextView textViewContent2;
        LinearLayout linearLayoutScore2;


        ViewHolder(View view, int type) {
            switch (type) {
                case 0:
                    textviewTitle = (TextView) view.findViewById(R.id.textview_title);
                    textviewLevel = (TextView) view.findViewById(R.id.textview_level);
                    score = (TextView) view.findViewById(R.id.score);
                    scoreUsedTime = (TextView) view.findViewById(R.id.score_used_time);
                    relativeLayoutDetail = (RelativeLayout) view.findViewById(R.id.relativeLayout_detail);
                    textviewEmpty = (TextView) view.findViewById(R.id.textview_empty);
                    linearLayoutScore = (LinearLayout) view.findViewById(R.id.linearlayout_score);
                    break;
                case 1:
                    questionTitle = (TextView) view.findViewById(R.id.question_title);
                    roundProcess = (MagnificentChart) view.findViewById(R.id.round_process);
                    countRight = (TextView) view.findViewById(R.id.count_right);
                    countWrong = (TextView) view.findViewById(R.id.count_wrong);
                    countMiss = (TextView) view.findViewById(R.id.count_miss);
                    btnProcessDetail = (RelativeLayout) view.findViewById(R.id.btn_process_detail);
                    break;
                case 2:
                    textViewContent2 = (TextView) view.findViewById(R.id.textview_content_2);
                    textviewTitle2 = (TextView) view.findViewById(R.id.textview_title_2);
                    textviewLevel2 = (TextView) view.findViewById(R.id.textview_level_2);
                    score2 = (TextView) view.findViewById(R.id.score_2);
                    scoreUsedTime2 = (TextView) view.findViewById(R.id.score_used_time_2);
                    relativeLayoutDetail2 = (RelativeLayout) view.findViewById(R.id.relativeLayout_detail_2);
                    textViewEmpty2 = (TextView) view.findViewById(R.id.textview_empty_2);
                    linearLayoutScore2 = (LinearLayout) view.findViewById(R.id.linearLayout_score_2);
                    break;
            }
        }
    }
}
